#!/bin/bash
# File Name: check_monitor_pt.sh
# Author: moshan
# mail: mo_shan@yeah.net
# Created Time: 2019-02-22 18:44:10
# Function: 检查pt工具执行进度的可用性,只有都ok才建议使用monitor_pt.sh
#########################################################################
:<<comment
变量注释:
db_name       : 业务库的库名，一般为'mafengwo'，要根据实际情况配置;
mysql_user    : 连接mysql用户名;
mysql_passwd  : 连接mysql的密码;
log_file      : 日志文件;
mysql_host    : 节点的ip地址
mysql_port    : mysql端口号
comment
db_name="mafengwo"
mysql_user="moshan"
mysql_passwd="xxxxxxxxx"
mysql_host="192.168.3.xx"
mysql_port="3306"
 
#以下无需编辑修改
mysql_comm="mysql -u${mysql_user} -p${mysql_passwd} -h${mysql_host} -P${mysql_port} ${db_name}"
table_list=($*)



if [ "${#table_list[@]}x" == "0x" ]
then
    echo "bash ${0} \"table list\""
    [ ! -s "table.log" ] && exit || table_list=($(cat table.log))
fi
for ((i=0;i<${#table_list[@]};i++))
do
    table_ok1=$(${mysql_comm} -NBe "show tables"|grep -c "^_${table_list[${i}]}_new$")
    table_ok2=$(${mysql_comm} -NBe "show tables"|grep -c "^_${table_list[${i}]}_old$")
    if [ "${table_ok1}x" == "0x" -a "${table_ok2}x" == "0x" ]
    then
        echo -e "\033[32m[$(date "+%F %H:%M:%S")] [OK] ${table_list[${i}]}\033[0m"
    else
        [ "${table_ok1}x" != "0x" ] && show_info="Table '_${table_list[${i}]}_new' already exists."
        [ "${table_ok2}x" != "0x" ] && show_info="${show_info}Table '_${table_list[${i}]}_old' already exists."
        echo -e "\033[31m[$(date "+%F %H:%M:%S")] [ERROR] ${table_list[${i}]}:The reason is that ${show_info}\033[0m"
        show_info=""
    fi
done
